package controller;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

import pojo.Binhluan;
import pojo.Hoadon;
import pojo.Loaisanpham;
import pojo.Quangcao;
import pojo.Sanpham;
import util.BeanUtil;
import util.ConvertHelper;
import util.MessageNotificationHepler;
import util.SearchVisitor;
import dao.BinhluanDAO;
import dao.HoadonDAO;
import dao.LoaisanphamDAO;
import dao.QuangcaoDAO;
import dao.SanphamDAO;

@ManagedBean(name = "quanLyBinhLuanBean")
@ViewScoped
public class QuanLyBinhLuanBean {

	private List<Binhluan> lstBinhLuan = new ArrayList<Binhluan>();

	private List<Loaisanpham> lstLoaiSanPham = ((HeaderBean) BeanUtil
			.findBean("headerBean")).getLstLoaiSanPham();
	private List<Sanpham> lstSanPham = new ArrayList<Sanpham>();
	private String chuoiTimKiem;
	private int maNhomLietKe;
	private int maLoaiLietKe;
	private int trangThaiDuyet;
	private int trangThaiXoa;

	private int maBinhLuan;
	private String noiDungBinhLuan;

	public void LietKeToanBo() {
		BinhluanDAO bldao = new BinhluanDAO();
		setLstBinhLuan(bldao.getList());
	}

	public void LietKeTheoNhuCau() {

		ClearLstSanPham();
		ClearLstBinhLuan();

		System.out.println("Liet Ke Nhu Cau: " + getMaNhomLietKe() + " -- "
				+ getMaLoaiLietKe() + " -- " + getTrangThaiDuyet() + " -- "
				+ getTrangThaiXoa());

		BinhluanDAO bldao = new BinhluanDAO();

		if (-1 == getMaNhomLietKe())
			setLstBinhLuan(bldao.GetBinhLuanTheoMaLoaiVaTrangThai(
					getMaNhomLietKe(), getTrangThaiDuyet(), getTrangThaiXoa()));
		else
			setLstBinhLuan(bldao.GetBinhLuanTheoMaLoaiVaTrangThai(
					getMaLoaiLietKe(), getTrangThaiDuyet(), getTrangThaiXoa()));

		setLstBinhLuan(bldao.removeDuplicate(getLstBinhLuan()));
		// Chưa liệt kê:
	}

	public void changeNhomListener(AjaxBehaviorEvent event) {
		System.out.println("listener: called by "
				+ event.getComponent().getClass().getName());

		HtmlSelectOneMenu combobox = (HtmlSelectOneMenu) event.getComponent();
		int contentValue = (int) combobox.getValue();

		System.out.println(combobox.getId() + " -- " + contentValue);

		setMaNhomLietKe(contentValue);
		ThayDoiGiaTriLoaiSanPham();
	}

	public void ThayDoiGiaTriLoaiSanPham() {
		System.out.println("Thay doi gia tri Loai SP: " + getMaNhomLietKe());
		if (-1 == maNhomLietKe)
			lstLoaiSanPham = ((HeaderBean) BeanUtil.findBean("headerBean"))
					.getLstLoaiSanPham();
		else {
			lstLoaiSanPham = (new LoaisanphamDAO())
					.GetLoaiSanPhamTheoMaNhom(maNhomLietKe, false);
		}
	}

	public void TimKiemSanPham() {

		System.out.println("Tim Kiếm SP: ");
		SanphamDAO spdao = new SanphamDAO();
		SearchVisitor sv = new SearchVisitor();
		spdao.accept(sv, getChuoiTimKiem(), 0, -1);
		setLstSanPham(sv.getResultSanPham());
		System.out.println("Tim Kiếm SP KQ: " + getLstSanPham().size());
	}

	public void TimKiemBinhLuan(Sanpham sp) {

		System.out.println("Tim Kiếm BL: " + sp.getTensanpham());
		lstBinhLuan = new ArrayList<Binhluan>(sp.getBinhluans());
		System.out.println("Tim Kiếm BL KQ: " + getLstBinhLuan().size());

		// -----------
		ClearLstSanPham();
		getLstSanPham().add(sp);
	}

	public void XoaThongTinDangLuuTru() {
		setMaBinhLuan(-1);
		setNoiDungBinhLuan(null);
	}

	public void SuaBinhLuan() {
		System.out.println("Sửa: " + getMaBinhLuan() + "-" + getNoiDungBinhLuan());
		BinhluanDAO bldao = new BinhluanDAO();
		Binhluan bl = bldao.get(getMaBinhLuan());
		bl.setNoidung(getNoiDungBinhLuan());

		bldao.saveOrUpdate(bl);

		XoaThongTinDangLuuTru();

		MessageNotificationHepler.ShowMessage(String.format(
				"Bình luận số '%s' đã được cập nhật thành công!",
				bl.getMabinhluan()));
	}

	public void Xoa(Binhluan bl) {
		System.out.println("Xóa: " + bl.getMabinhluan());
		BinhluanDAO bldao = new BinhluanDAO();
		bl.setDaxoa(true);
		bldao.saveOrUpdate(bl);
		ClearLstBinhLuan();

		MessageNotificationHepler
				.ShowMessage(String
						.format("Bình luận với mã số '%s' cho Sản phẩm '%s' đã được Xóa thành công!",
								bl.getMabinhluan(), bl.getSanpham()
										.getTensanpham()));
	}

	public void HuyXoa(Binhluan bl) {
		System.out.println("Hủy Xóa: " + bl.getMabinhluan());
		BinhluanDAO bldao = new BinhluanDAO();
		bl.setDaxoa(false);
		bldao.saveOrUpdate(bl);
		ClearLstBinhLuan();

		MessageNotificationHepler
				.ShowMessage(String
						.format("Bình luận với mã số '%s' cho Sản phẩm '%s' đã được Phục hồi thành công!",
								bl.getMabinhluan(), bl.getSanpham()
										.getTensanpham()));
	}

	public void Duyet(Binhluan bl) {
		System.out.println("Duyệt: " + bl.getMabinhluan());
		BinhluanDAO bldao = new BinhluanDAO();
		bl.setDaduyet(true);
		bldao.saveOrUpdate(bl);
		ClearLstBinhLuan();

		MessageNotificationHepler
				.ShowMessage(String
						.format("Bình luận với mã số '%s' cho Sản phẩm '%s' đã được Duyệt thành công!",
								bl.getMabinhluan(), bl.getSanpham()
										.getTensanpham()));
	}

	public void HuyDuyet(Binhluan bl) {
		System.out.println("Hủy Duyệt: " + bl.getMabinhluan());
		BinhluanDAO bldao = new BinhluanDAO();
		bl.setDaduyet(false);
		bldao.saveOrUpdate(bl);
		ClearLstBinhLuan();

		MessageNotificationHepler
				.ShowMessage(String
						.format("Bình luận với mã số '%s' cho Sản phẩm '%s' đã được Hủy duyệt thành công!",
								bl.getMabinhluan(), bl.getSanpham()
										.getTensanpham()));
	}

	private void ClearLstBinhLuan() {
		getLstBinhLuan().clear();
	}

	private void ClearLstSanPham() {
		getLstSanPham().clear();
	}

	public void SetMaBinhLuanAndGetData(int maBinhLuan) {
		this.setMaBinhLuan(maBinhLuan);
		BinhluanDAO bldao = new BinhluanDAO();
		Binhluan bl = bldao.get(maBinhLuan);
		setNoiDungBinhLuan(bl.getNoidung());
		System.out.println("Lấy thông tin: " + bl.getMabinhluan());
	}

	// --------------------------------
	public List<Binhluan> getLstBinhLuan() {
		return lstBinhLuan;
	}

	public void setLstBinhLuan(List<Binhluan> lstBinhLuan) {
		this.lstBinhLuan = lstBinhLuan;
	}

	public List<Loaisanpham> getLstLoaiSanPham() {
		return lstLoaiSanPham;
	}

	public void setLstLoaiSanPham(List<Loaisanpham> lstLoaiSanPham) {
		this.lstLoaiSanPham = lstLoaiSanPham;
	}

	public List<Sanpham> getLstSanPham() {
		return lstSanPham;
	}

	public void setLstSanPham(List<Sanpham> lstSanPham) {
		this.lstSanPham = lstSanPham;
	}

	public String getChuoiTimKiem() {
		return chuoiTimKiem;
	}

	public void setChuoiTimKiem(String chuoiTimKiem) {
		this.chuoiTimKiem = chuoiTimKiem;
	}

	public int getMaNhomLietKe() {
		return maNhomLietKe;
	}

	public void setMaNhomLietKe(int maNhomLietKe) {
		this.maNhomLietKe = maNhomLietKe;
	}

	public int getMaLoaiLietKe() {
		return maLoaiLietKe;
	}

	public void setMaLoaiLietKe(int maLoaiLietKe) {
		this.maLoaiLietKe = maLoaiLietKe;
	}

	public int getTrangThaiDuyet() {
		return trangThaiDuyet;
	}

	public void setTrangThaiDuyet(int trangThaiDuyet) {
		this.trangThaiDuyet = trangThaiDuyet;
	}

	public int getTrangThaiXoa() {
		return trangThaiXoa;
	}

	public void setTrangThaiXoa(int trangThaiXoa) {
		this.trangThaiXoa = trangThaiXoa;
	}

	public int getMaBinhLuan() {
		return maBinhLuan;
	}

	public void setMaBinhLuan(int maBinhLuan) {
		this.maBinhLuan = maBinhLuan;
	}

	public String getNoiDungBinhLuan() {
		return noiDungBinhLuan;
	}

	public void setNoiDungBinhLuan(String noiDungBinhLuan) {
		this.noiDungBinhLuan = noiDungBinhLuan;
	}

}
